package com.oig.sys.auth.service.impl;

import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.dao.ITbJobDao;
import com.oig.sys.auth.dao.ITbUserJobDao;
import com.oig.sys.auth.dto.JobDto;
import com.oig.sys.auth.entity.TbUser;
import com.oig.sys.auth.entity.TbUserJob;
import com.oig.sys.auth.service.ITbUserJobService;
import com.oig.sys.auth.vo.JobVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbUserJobServiceImpl implements ITbUserJobService {

	private final ITbUserJobDao tbUserJobDao;
	private final ITbJobDao jobDao;

	@Autowired
	public TbUserJobServiceImpl( ITbUserJobDao tbUserJobDao,ITbJobDao jobDao){
		 this.tbUserJobDao = tbUserJobDao;
		 this.jobDao = jobDao;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public List<JobVo> addUserJob(TbUser user, List<JobDto> jobDtoList) {
		List<Integer> jobIdList = jobDtoList.stream().map(JobDto::getId).collect(Collectors.toList());
		List<JobVo> jobVoList = this.jobDao.findAllByIds(jobIdList);
		Map<Integer,JobVo> jobVoMap = jobVoList.stream().collect(Collectors.toMap(JobVo::getId, Function.identity()));
		for (JobDto jobDto : jobDtoList){
			if (jobDto.getId()==null){
				throw new BadRequestException("岗位id为空");
			}
			JobVo jobVo = jobVoMap.get(jobDto.getId());
			if (!jobVo.getTenantId().equals(user.getTenantId())){
				throw new BadRequestException("岗位和用户不属于同一个租户") ;
			}
			TbUserJob userJob = new TbUserJob();
			userJob.setJobId(jobDto.getId());
			userJob.setUserId(user.getId());
			userJob.setCreateTime(new Date());
			this.tbUserJobDao.save(userJob) ;
			jobIdList.add(jobDto.getId()) ;
		}
		return jobVoList;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public List<JobVo> updateUserJob(TbUser user, List<JobDto> jobDtoList) {
		List<Integer> jobIdList = jobDtoList.stream().map(JobDto::getId).collect(Collectors.toList());
		List<Integer> userJobIdList = new ArrayList<>() ;
		List<JobVo> jobVoList = this.jobDao.findAllByIds(jobIdList);
		Map<Integer,JobVo> jobVoMap = jobVoList.stream().collect(Collectors.toMap(JobVo::getId, Function.identity()));
		for (JobDto jobDto : jobDtoList) {
			if (jobDto.getId()==null){
				throw new BadRequestException("岗位id为空");
			}
			TbUserJob userJob = this.tbUserJobDao.getByUserIdAndJobId(user.getId(), jobDto.getId()) ;
			if (userJob==null){
				JobVo jobVo = jobVoMap.get(jobDto.getId());
				if (!jobVo.getTenantId().equals(user.getTenantId())){
					throw new BadRequestException("岗位和用户不属于同一个租户") ;
				}
				userJob = new TbUserJob();
				userJob.setJobId(jobDto.getId());
				userJob.setUserId(user.getId());
				userJob.setCreateTime(new Date());
				this.tbUserJobDao.save(userJob) ;
			}
			userJobIdList.add(userJob.getId()) ;
			jobIdList.add(jobDto.getId()) ;
		}
		List<Integer> delIdList = this.tbUserJobDao.findAllIdByUserIdAndIdNotIn(user.getId(), userJobIdList);
		log.debug("user job delIdList={}",delIdList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbUserJobDao.deleteByIdIn(delIdList);
		}
		return jobVoList;
	}


	@Override
	public List<JobVo> getUserJob(Integer userId) {
		List<Integer> jobIdList = this.tbUserJobDao.findJobIdByUserId(userId) ;
		return this.jobDao.findAllByIds(jobIdList);
	}

	@Transactional
	@Override
	public void delByUserId(Integer userId) {
		List<Integer> delIdList = this.tbUserJobDao.findAllIdByUserId(userId) ;
		if (delIdList!=null &&!delIdList.isEmpty()){
			this.tbUserJobDao.deleteByIdIn(delIdList);
		}
	}

	@Transactional
	@Override
	public void delByJobId(Integer jobId) {
		List<Integer> delIdList = this.tbUserJobDao.findAllIdByJobId(jobId) ;
		if (delIdList!=null &&!delIdList.isEmpty()){
			this.tbUserJobDao.deleteByIdIn(delIdList);
		}
	}
}
